package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.HoldingFundEntity;

import java.util.List;

/**
 * Created by sxy on 2017/11/2.
 */

public class HoldingFundListAdapter extends BaseAdapter {
    private List<HoldingFundEntity> mList;
    private Context mContext;

    public HoldingFundListAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<HoldingFundEntity> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public HoldingFundEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_holding_fund_list, null);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.tv_current_value = (TextView) convertView.findViewById(R.id.tv_current_value);
            holder.tv_nav_title = (TextView) convertView.findViewById(R.id.tv_nav_title);
            holder.tv_nav = (TextView) convertView.findViewById(R.id.tv_nav);
            holder.tv_day = (TextView) convertView.findViewById(R.id.tv_day);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        HoldingFundEntity entity = mList.get(position);
        holder.tv_name.setText(entity.getFundName());
        if (entity.isGroup()) {
            holder.tv_type.setBackgroundResource(R.drawable.zuhe);

            holder.tv_nav_title.setText("基金数量");
            holder.tv_nav.setText(entity.getSize() + "");
        } else {
            if (entity.getFundType() != null && entity.getFundType().equals("fund"))
                holder.tv_type.setBackgroundResource(R.drawable.personal_center_jijin);
            else if (entity.getFundType() != null && entity.getFundType().equals("collective"))
                holder.tv_type.setBackgroundResource(R.drawable.personal_center_licai);

            holder.tv_nav_title.setText("最新净值");
            holder.tv_nav.setText(entity.getNav());
        }

        holder.tv_current_value.setText(entity.getCurrentValue());

        holder.tv_day.setText(entity.getPreviousProfitNLoss());
        return convertView;
    }

    class Holder {
        TextView tv_type;
        TextView tv_name;
        TextView tv_current_value;
        TextView tv_nav_title;
        TextView tv_nav;
        TextView tv_day;
    }
}